﻿using B.S.DataBase.MyProject.Domain.ChronicDiseaseManagement;
using B.S.DataBase.MyProject.Domain.Infrastructures;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.ChronicCareCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.ChronicCareCommandHandler
{
    /// <summary>
    /// 删除复诊计划命令处理方法
    /// </summary>
    public class DelFollowUpPlanCommandHandler:IRequestHandler<DelFollowUpPlanCommand,APIResult<int>>
    {
        private readonly SqlSugarContext sugar;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sugar"></param>
        public DelFollowUpPlanCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 删除复诊计划
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(DelFollowUpPlanCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {

                var model = await sugar.db.Queryable<FollowUpPlan>().Where(x => x.FollowUpPlanId == request.FollowUpPlanId).FirstAsync();

                var r = await sugar.db.Deleteable(model).ExecuteCommandAsync();
                if (r > 0)
                {
                    result.Code = APIEnum.删除成功;
                    result.Msg = APIEnum.删除成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.删除失败;
                    result.Msg = APIEnum.删除失败.ToString();
                    result.data = r;
                }


            }
            catch (Exception ex)
            {

                // 记录异常信息
                Console.WriteLine($"删除复诊计划数据时发生异常: {ex.Message}");
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);
        }


    }
}
